<template>
  <div class="goods">
    <van-list
      class="goods-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      offset="0"
    >
      <div
        class="goods-list-item"
        v-for="(item, index) in goods"
        :key="index"
        @click="goDetail('/home/goodsinfo', item.id)"
      >
        <div class="img-box">
          <img
            v-lazy="item.img_url"
            alt=""
          />
        </div>
        <h2 class="title">{{ item.title }}</h2>
        <div class="info">
          <div class="desc">主动降噪</div>
          <div class="sell">热卖中</div>
        </div>
      </div>
    </van-list>
  </div>
</template>
<script>
// import mixins from '@/mixins';
export default {
  // mixins: [mixins],
  data() {
    return {
      goods: [],
      page: 0,
      limit: 6,
      loading: false,
      finished: false,
      count: 0,
    };
  },
  created() {
    console.log('goodslist created');
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // setTimeout(async () => {
      await this.getGoods();

      // 加载状态结束
      this.loading = false;
      console.log(this.count);

      // 数据全部加载完成
      if (this.goods.length >= this.count) {
        this.finished = true;
      }
      // }, 1000);
    },
    async getGoods() {
      ++this.page;
      const res = await this.$http.getGoods({ page: this.page, limit: this.limit });
      this.goods = [...this.goods, ...res.data.message];
      this.count = res.data.count;
    },
  },
};
</script>
<style lang="scss">
.goods {
  padding: 10px;
  box-sizing: border-box;
  .goods-list {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .goods-list-item {
      width: 48%;
      border: 1px solid #eee;
      margin-bottom: 10px;
      text-align: center;
      font-size: 12px;
      padding: 10px;
      box-sizing: border-box;
      overflow: hidden;
      .img-box {
        width: 164px;
        height: 164px;
        img {
          width: 164px;
          height: 164px;
        }
      }
      .title {
        font-size: 14px;
      }
      .info {
        .desc {
          margin-bottom: 10px;
        }
      }
    }
  }
  .van-list__error-text,
  .van-list__finished-text,
  .van-list__loading {
    width: 100%;
    text-align: center;
  }
}
</style>
